package com.FH.permission.member_file;

import com.FH.organization.fileupload.FileUploadDTO;
import com.FH.organization.remind.RemindPullDTO;
import com.FH.organization.remind.RemindResource;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Stateless
public class MemberFileDAOImpl extends AccessDB implements MemberFileDAO {

    @Inject
    RemindResource remindResource;

    @Override
    public ResultDTO addMemberFile(MemberFilePullDTO memberFilePullDTO) {

        ResultDTO resultDTO = new ResultDTO();

        int resultls = 0;

        if(null!=memberFilePullDTO.getMember_list()&&!"".equals(memberFilePullDTO.getMember_list())){

            try {
                JSONArray jsonArray = new JSONArray(memberFilePullDTO.getMember_list());

                for(int i = 0; i< jsonArray.length(); i++){

                    if(null!= jsonArray.getJSONObject(i).getString("member_id")&&!"".equals(jsonArray.getJSONObject(i).getString("member_id"))){

                        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

                        String sql = "insert into member_file (member_file_id,file_id,member_id) values " +
                                "('" + uuid + "','" + memberFilePullDTO.getFile_id() + "','" + jsonArray.getJSONObject(i).getString("member_id") + "')";
                        resultls = this.addReportingTo(sql);

                        //文件分享提醒
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                        RemindPullDTO remindPullDTO = new RemindPullDTO();
                        remindPullDTO.setRemind_content("您有一条文件共享消息");
                        remindPullDTO.setRemind_time(df.format(new Date()));
                        remindPullDTO.setRemind_type("25");
                        remindPullDTO.setRemind_form_id(memberFilePullDTO.getFile_id());
                        remindPullDTO.setRemind_order("0");
                        remindPullDTO.setCreate_time(df.format(new Date()));
                        remindPullDTO.setRemind_endtime("");
                        remindPullDTO.setRemind_member_id(jsonArray.getJSONObject(i).getString("member_id"));
                        remindPullDTO.setRemind_starttime("");
                        remindPullDTO.setRemind_descritpion("您有一条文件共享内容！");
                        remindResource.addRemindInfo(remindPullDTO);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        if (resultls != 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion("文件分享成功");
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion("文件分享失败");
        }

        return resultDTO;
    }

    @Override
    public ResultDTO getFileById(String member_id) {

        ResultDTO resultDTO = new ResultDTO();

        String sql = "SELECT DISTINCT mf.file_id,f.file_addr,f.file_time,f.filegui_id,f.file_name FROM member_file mf " +
                "JOIN file f ON mf.file_id = f.file_id WHERE mf.member_id = '" + member_id + "' ";

        List<String[]> results = new ArrayList<String[]>();

        List<FileUploadDTO> fileList = new ArrayList<FileUploadDTO>();

        results = this.getReportingTo(sql);

        for(int i=0;i<results.size();i++){
            FileUploadDTO fileUploadDTO = new FileUploadDTO();
            fileUploadDTO.setFile_id(results.get(i)[1]);
            String[] image_path = this.getImage_path();
            String image_pathSerAddr = image_path[2];//文件访问地址
            fileUploadDTO.setFile_addr(image_pathSerAddr+results.get(i)[2]);
            fileUploadDTO.setFile_time(results.get(i)[3]);
            fileUploadDTO.setFilegui_id(results.get(i)[4]);
            fileUploadDTO.setFiletype(results.get(i)[5]);

            fileList.add(fileUploadDTO);
        }

        if (results.size() > 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion("文件获取成功");
            resultDTO.setValues(fileList);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion("文件获取失败");
        }

        return resultDTO;
    }

    /**
     * 删除被分享的文件(删除权限)
     *
     * @param memberFilePullDTO
     */
    @Override
    public ResultDTO deleteMemberFile(MemberFilePullDTO memberFilePullDTO) {

        ResultDTO resultDTO = new ResultDTO();

        //删除之前查询一下该表是否有记录

        String sql = "SELECT count(1) FROM member_file WHERE file_id = '" + memberFilePullDTO.getFile_id() + "'" +
                " AND member_id = '" + memberFilePullDTO.getMember_list() + "'";

        String exist = getReportingToOne(sql);

        int result = 0;
        if(!exist.equals("0")){
            String delete = "DELETE FROM member_file WHERE file_id = '" + memberFilePullDTO.getFile_id() + "'" +
                    " AND member_id = '" + memberFilePullDTO.getMember_list() + "'";
            result = addReportingTo(delete);
        }

        if(result!=0){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        }else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
